﻿using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data;

namespace 门诊医生工作站
{
    public partial class frm_Login : Form
    {
        public static string _No;
        public frm_Login()
        {          
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            SqlConnection sqlConnection = new SqlConnection();//声明并实例化SQL连接
            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;//借助App.config连接数据库
            SqlCommand sqlCommand2 = new SqlCommand();
            DataTable dataTable = new DataTable();//保存一整张表
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.SelectCommand = sqlCommand2;//接收查询结果
            sqlCommand2.Connection = sqlConnection;
            sqlCommand2.CommandText = $"SELECT * FROM dbo.tb_Office";
            sqlConnection.Open();
            sqlDataAdapter.Fill(dataTable);//将接收到的结果填充到dataTable里面
            this.cmb_Office.DataSource = dataTable;
            this.cmb_Office.ValueMember = "No";//选中的No对应OfficeNo
            this.cmb_Office.DisplayMember = "Name";//要显示的列名称
            sqlConnection.Close();
        }
       //登录按钮
        private void btn_Login_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();//声明并实例化SQL连接
            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
            SqlCommand sqlCommand = new SqlCommand();//定义查询用户表的sqlCommend
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText = $"SELECT COUNT(1) FROM tb_User WHERE No=@No AND Password=HASHBYTES('MD5',@Password) AND Name=@Name AND OfficeNo=@OfficeNo ; ";                               
            sqlCommand.Parameters.AddWithValue("@No", this.txb_No.Text);
            sqlCommand.Parameters.AddWithValue("@Password", this.txb_Password.Text);
            sqlCommand.Parameters.AddWithValue("@Name", this.txb_Name.Text);
            sqlCommand.Parameters.AddWithValue("@OfficeNo",this.cmb_Office.SelectedValue);
            sqlCommand.Parameters["@Password"].SqlDbType = System.Data.SqlDbType.VarChar;
           
            sqlConnection.Open();         //打开SQL连接
            int rowCount = (int)sqlCommand.ExecuteScalar();//返回查询结果的第一行第一列
            sqlConnection.Close();        //关闭SQL连接
            if (rowCount==1)
            {
                MessageBox.Show("登录成功！");
                _No = this.txb_No.Text.Trim();
                this.DialogResult = DialogResult.OK;
                this.Close();             
            }
            else
            {
                MessageBox.Show("登录失败！");
                this.txb_Password.Focus();
                this.txb_Password.SelectAll();
            }
        }
       //注册按钮
        private void btn_SignUp_Click(object sender, EventArgs e)
        {
            frm_SignUp frm_SignUp = new frm_SignUp();
            frm_SignUp.ShowDialog();
            
        }
       //退出系统按钮
        private void btn_Logout_Click(object sender, EventArgs e)
        {
            DialogResult messageboxResult= MessageBox.Show("确定退出系统吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (messageboxResult==DialogResult.Yes)
            {
                MessageBox.Show("即将退出系统！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            this.Close();
        }
    }
}
